<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Tool\SMS\SendTemplateSMS;
use App\Models\M3Result;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Support\Facades\Auth;
use App\Mobile;
class AuthController extends Controller
{
    protected $username = 'mobile'; # 与 password 字段配合登陆的字段 ,name,email,mobile 都可以
    protected $redirectPath = '/'; # 登陆成功后的跳转方向
    protected $redirectAfterLogout = '/'; # 默认退出后跳转页
    protected $loginPath = 'auth/login'; # 默认登陆 URL
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    // protected $username = 'name';   //默认是用email登录，改成用用户名登录，
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }
    // protected $username = 'name';   //与 password 字段配合登陆的字段 ,name,email,mobile 都可以
    // protected $redirectPath = '/'; // 登陆成功后的跳转方向
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
            'mobile'=>'required|regex:/^1[34587]\d{9}$/',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'mobile' => $data['mobile'],
            'regtime' => time(),    //添加了mobile字段
            // 'desc' => $data['desc'],
            // 'works' => $data['works']
        ]);
    }
     public function postRegister(Request $request)
    {
        $shoujihao = $request->input('mobile');
        $duanxin = $request->input('duanxin');
        $mobile =Mobile::where('mobile',$shoujihao)->select('token')->get();
        foreach ($mobile as $key => $value) {
            $arr = $value['token'];
        }

        $validator = $this->validator($request->all());
        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }
        if ($duanxin == '') {
          return '短信验证码不能为空';
        }
        if($duanxin !== $arr){
            return '短信验证码不正确';
        }
        Auth::login($this->create($request->all()));

        return redirect($this->redirectPath());
    }

}
